<?php
class Account_groups_model extends Admin_Model {

	//public $module = 'account_groups';
	public $table = ADMIN_GROUP_TABLE;

	function __construct(){
		
		// set field cần kiểm tra tên
		$this->checkExistField = array(
			'name'	=>	'name',
		);

	}

	
	function saveManagement($result_perm=''){
		if($this->input->post('statusAdmincp')=='on'){
			$status = 1;
		}else{
			$status = 0;
		}

		if($this->input->post('hiddenIdAdmincp')==0){
			$data = array(
				'name'=> $this->input->post('nameAdmincp'),
				'permission'=> $result_perm,
				$this->status_name => $status,
				'created'=> date('Y-m-d H:i:s',time())
			);
			if($this->db->insert($this->table,$data)){
				modules::run('admincp/saveLog',$this->module,$this->db->insert_id(),'Add new','Add new');
				return true;
			}
		}else{
			$result = $this->getDetailManagement($this->input->post('hiddenIdAdmincp'));
			$data = array(
				'name'=> $this->input->post('nameAdmincp'),
				'permission'=> $result_perm,
				$this->status_name => $status
			);
			modules::run('admincp/saveLog',$this->module,$this->input->post('hiddenIdAdmincp'),'','Update',$result,$data);
			$this->db->where('id',$this->input->post('hiddenIdAdmincp'));
			if($this->db->update($this->table,$data)){
				return true;
			}
		}
		return false;
	}
	
	function list_groups(){
		$this->db->select('*');
		$this->db->order_by('name','ASC');
		$query = $this->db->get($this->table);

		if($query->result()){
			return $query->result();
		}else{
			return false;
		}
	}
}